//==============================================================================
//
// Title:       BIST_SGMIIloopbackTest.c
// Purpose:     A short description of the implementation.
//
// Created on:  2013-3-1 at 15:00:35 by Vinson.Zhang
// Copyright:   dafda. All Rights Reserved.
//
//==============================================================================
#include "RFBB.h"         
#include <ansi_c.h>

_FRMON_TEST_STRUCT_CMD g_cFRMONSGMIIloopbackTestCmd[4] = {{"FRMon,action,load,agent,MURKKU2Access,Vegas,_MURKKU2Access","done"},
                                                        {"FRMon/Vegas/_MURKKU2Access,action,init,MURKKU2Access,","done"},
                                                        {"FRMon/Vegas/_MURKKU2Access,action,sgmiiLoopBackTest","0x00"},
                                                        {"FRMon/Vegas/_MURKKU2Access,action,terminate,MURKKU2Access","done"}};
/**************************************************************************************************
  Function:         BIST_SGMIIloopbackTest
  Description:      This test aims to check that temperature sensors are undamaged.
  Calls:            none
  Called By:        dll
  Table Accessed:   none
  Table Updated:    none
  Input:            none
  Output:           error code
  Return:           none
  Test Case ID:     FRHD_SGMIIloopbackTest
  Used Command:     g_cFRMONSGMIIloopbackTestCmd
**************************************************************************************************/
void __decldllexp BIST_SGMIIloopbackTest(tTestData *testData, tTestError *testError)
{
    
    
    ErrMsg errMsg = {'\0'};
    ERRORINFO errorInfo;   
    ErrMsg szLogMsg;
    CAObjHandle hStepName = 0;   
    char* pszStepName = NULL;    
    char kszShmonAgent[BUFFER_SIZE_256] ={'\0'};
    char cChannel[BUFFER_SIZE_256] ={'\0'};
    int iPipeNum = 0;
    int iTempType = 0;
    int iPassFail = 1;
	int iReturn = 0;
	int error = 0;
    int iArryNum = 0;
    //////////////////////////////////////////////////////////////////////////   
    // Resolve StepName 
    tsErrChk(TS_SeqContextGetProperty (testData->seqContextCVI, &errorInfo, 
                                       TS_SeqContextStep, CAVT_OBJHANDLE, 
                                       &hStepName));
    tsErrChk(TS_StepGetProperty (hStepName, &errorInfo, TS_StepName, CAVT_CSTRING, 
                                 &pszStepName));
   
    sprintf( szLogMsg, TEST_NAME_EVENT_FMTSTR, pszStepName );
    ErrChk( EELog_INEventLogLogEvent( g_hEventLog, NULL, szLogMsg ) );
   
    // Post message to GUI
    tsErrChk(TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                       TS_UIMsg_ProgressText, 0.0,
                                       pszStepName, 0, VTRUE));
    
    
     //InetTelnetOpen(FRCommand)
    g_hFrmonAgent = RfbbInetTelnetOpen (g_szDUTIP, g_iPort, 0);     

    for(iArryNum = 0;iArryNum < 3;iArryNum++)
    	{
        sprintf(kszShmonAgent,g_cFRMONSGMIIloopbackTestCmd[iArryNum].cWriteFrmonCmd);    
        FRmonAgent_String(kszShmonAgent, -1,g_cFRMONSGMIIloopbackTestCmd[iArryNum].cReturnValue, &iPassFail);
        if (iPassFail!=1) 
        	{
            goto Error;
        	}
		Delay(2);
    	}    
    
Error: 

#if(ON_OFF != SIMU_VERS)
    //action terminate
    sprintf(kszShmonAgent,g_cFRMONSGMIIloopbackTestCmd[3].cWriteFrmonCmd);
    Frmon_RunCmdNoAck(kszShmonAgent, 1); 


    if(iPassFail == PASS) 
    	{
        testData->result = PASS;      
    	}
    else 
    	{
        testData->result = FAIL;
    	}   

	//InetTelnetClose(FRCommand)
    InetTelnetClose(g_hFrmonAgent);
	
    //FREE RESOURCES
    CA_DISCARD_OBJ( hStepName );
    CA_FREE( pszStepName );

    // If an error occurred, set the error flag to cause a run-time error in TestStand.
    if (error < 0)
    	{
        testError->errorFlag = TRUE;
        testError->errorCode = error;
        testData->replaceStringFuncPtr(&testError->errorMessage, errMsg);
    	}

#else

#endif
    // Clear message from GUI
    TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                 TS_UIMsg_ProgressText, 0.0, " ", 0, VTRUE);

    return;    
}

